package lesson03;

import java.util.concurrent.TimeUnit;

/**
 *  可重入锁,锁是同一个对象，不同线程不可以重入，同一个线程才可以
 */
public class ReentrantLoock {

    synchronized void m1(){
        for (int i = 0; i < 10; i++) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i);
        }
    }

    synchronized void m2(){
        System.out.println("m2....");
    }

    public static void main(String[] args) {
        ReentrantLoock reeen = new ReentrantLoock();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                reeen.m1();
            }
        });
        t1.start();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->reeen.m2()).start(); // 这个会等t1线程执行完后才能执行
        //t1.join(new Thread(()->reeen.m2()).start());
    }
}
